home *** CD-ROM | disk | FTP | other *** search
- Path: ohstpy.mps.ohio-state.edu!vancleef
- From: vancleef@ohstpy.mps.ohio-state.edu
- Newsgroups: comp.lang.c
- Subject: Re: sscanf problems
- Message-ID: <1996Jan24.032124.8802@ohstpy>
- Date: 24 Jan 96 03:21:24 -0500
- References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu> <4e4h0j$af7@fountain.mindlink.net>
- Organization: The Ohio State University, Department of Physics
-
- In article <4e4h0j$af7@fountain.mindlink.net>, genew@mindlink.bc.ca (Gene Wirchenko) writes:
- > Chris Mongold <cmongold@magnus.acs.ohio-state.edu> wrote:
- >
- >>Hello,
- >> I'm sorry if this is an inappropriate topic, but I've tried
- >>everything else. I can't seem to get sscanf to to separate a string
- >>into various variable types. Here is an example:
- >
- >>#include <stdio.h>
- >
- >>void main()
- > ^
- > This is a no-no. Per the Standard, main() returns int.
- >
- >>{
- >>char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
- >>int type;
- >>FILE *fp;
- >
- >>printf("File: ");
- >>gets(input);
- >
- > Buffer can be overflowed. You might want to use fgets().
- >
- >>fp = fopen(input, "r");
- >
- > You don't check if the file could be opened. This may be the
- > problem. Add:
- >
- > if (fp==NULL)
- > {
- > printf("Input file could not be opened.\n");
- > exit(EXIT_FAILURE);
- > }
- >
- >>while(!feof(fp))
- >>{
- >>fgets(crap, 17, fp);
- >>sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
-
- ^^^^^ Try &type
-
-
- >>printf("%d %3s %6s %3s %3s", type, begin, segment, seg_len, load);
- > ^
- > type is never assigned a value.
- >
-
- He tries to do it above. sscanf requires the address of the target variable.
-
- >>}
- >
- > Since main() returns a value, return one. Add:
- > return 0;
- >
- >>}
- >>No matter what I do, it always 'bus errors' when I sscanf. I've tried
- >>it several different ways, including making crap larger, but to
- >>no avail. If anyone can help me, I would greatly appreciate it.
- >
- > I wonder if your data is in the wrong format. Include a copy of
- > it.
- > I suspect you'll need to copy the chars to their dest.
- >
- >>Please respond to cmongold@magnus.acs.ohio-state.edu
- >
- >>Thanks,
- >
- >>Chris
- >
- > Sincerely,
- >
- > Gene Wirchenko
- >
- > C Pronunciation Guide:
- > y=x++; "wye equals ex plus plus semicolon"
- > x=x++; "ex equals ex doublecross semicolon"
- >
-
- -Garrett
-
-
-
-